/**
 * Created by zhaoyoucai on 16/11/29.
 */
$('#showSide').click(function(){
        $("#aside").toggleClass("folded");
        //$(".app-header .navbar, .app-content").toggleClass("container");
        $(".app-header .navbar").toggleClass("container");
    })


    $(document).ready(function(){
        $(document).on('click','#action-toggle',function(){
            if(this.checked){
                $("input[name='post[]']").each(function(){
                    this.checked = true;
                });
            }else{
                $("input[name='post[]']").each(function(){
                    this.checked = false;
                });
            }
        });


    });

    $('.editStore').click(function(){
        var obj = $(this).parent().parent();
        $('#editStoreID').val($(this).attr('data-val'));
        var host = obj.children('td').eq(4).html();
        $('#editStoreHost').val(host.split('<br>')[0]);
        $('#editStoreServiceIP').val(host.split('<br>')[1]);
        $('#editStoreClientID').val(obj.children('td').eq(2).html());
        $('#editStoreAppSecret').val(obj.children('td').eq(5).html());
        $('#editStoreRefreshToken').val(obj.children('td').eq(6).html());
        $('#editStoreAccessToken').val(obj.children('td').eq(7).html());
        $('#editStoreRedirectURL').val(obj.children('td').eq(8).html());
        $('#editStoreType').val(obj.children('td').eq(3).attr('data-val'));
        $('#editStoreCompanyName').val(obj.children('td').eq(1).children('a').html());

        //console.log(obj.children('td').eq(1).html());

    });


    $('.addUser').click(function(){
        var storeID = $(this).attr('data-val');
        var obj = $(this).parent().parent();
        $('#addUserStoreID').val(storeID);
        $('#addUserStoreClientID').val(obj.children('td').eq(2).html());
        $('#addUserCompanyName').val(obj.children('td').eq(1).children('a').html());

        //$('#multiple').children('optgroup').html('');

         //$.getJSON("{% url 'system:getUserList' %}"+'?storeID='+storeID,function(data){
         $.getJSON("/system/api/getUserList"+'?storeID='+storeID,function(data){

             var result = data["Users"];
             $.each(result,function(idx,user){
                 //console.log(user.username);
                 line = '';
                 if(user.selected == true){
                     line += '<option selected="selected" value="'+user.username+'">'+user.first_name+'</option>';
                     //$('<li class="select2-selection__choice" title="'+user.first_name+'"><span class="select2-selection__choice__remove" role="presentation">×</span>'+user.first_name+'</li>').insertBefore(".select2-search--inline");
                 }else{
                     line += '<option value="'+user.username+'">'+user.first_name+'</option>';
                 }

                 $('#multiple').children('optgroup').append(line);

             });

         });
        //html +='</optgroup></select>';

        //$('#userSelect').append(html);

        //console.log(obj.children('td').eq(1).html());

    });

    $('.deleteStore').click(function(){
        $('#deleteStoreID').val($(this).attr('data-val'));
        $('#deleteStoreClientID').html($(this).parent().parent().children('td').eq(1).html()+'['+$(this).parent().parent().children('td').eq(2).html()+']');

    });

    function checkform(obj){
        var name = $('#storeClientID').val();
        if(name.length<=0){
            alert("Client ID不能为空！");
            $('#storeClientID').focus();
            return false;
        }
    }

    function checkUserForm(obj){
        var userlist = $('#multiple').val();
        if(userlist==null){
            alert('请选中用户！');
            $('#multiple').focus();
            return false;
        }
    }

    $('#selectCompany').change(function(){
        var id = $(this).val();
        location.href = "/system/company/"+id;
    });